using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using C1.Win.C1Input;
using C1.Win.C1List;
using C1.Win.C1TrueDBGrid;
using PCSComProcurement.Purchase.BO;
using PCSComProcurement.Purchase.DS;
using PCSComUtils.Common;
using PCSComUtils.Common.BO;
using PCSComUtils.MasterSetup.DS;
using PCSComUtils.PCSExc;
using PCSUtils.Log;
using PCSUtils.Utils;
using BeforeColEditEventArgs = C1.Win.C1TrueDBGrid.BeforeColEditEventArgs;
using C1DisplayColumn = C1.Win.C1TrueDBGrid.C1DisplayColumn;
using CancelEventArgs = System.ComponentModel.CancelEventArgs;
using ColEventArgs = C1.Win.C1TrueDBGrid.ColEventArgs;
using RowColChangeEventArgs = C1.Win.C1TrueDBGrid.RowColChangeEventArgs;
using PCSComUtils.DataContext;
using PCSComUtils.MasterSetup.BO;

namespace PCSProcurement.Purchase
{
	/// <summary>
	/// Summary description for POAdditionCharges.
	/// </summary>
	public class POAdditionCharges : Form
	{
		#region Form controls

		private Label lblTotalAmount;
		private TextBox txtTotalAmount;
		private TextBox txtTotalVATAmount;
		private Label lblTotalVATAmount;
		private GroupBox grbDistribution;
		private RadioButton radByManual;
		private RadioButton radByPrice;
		private RadioButton radByQuantity;
		private Button btnToDistribute;
		private Label lblDistributionAmount;
		private TextBox txtVendorCode;
		private Button btnDelete;
		private Button btnClose;
		private Button btnHelp;
		private Button btnSave;
		private TextBox txtTransDate;
		private TextBox txtCCN;
		private TextBox txtVendorName;
		private Label lblTransDate;
		private Label lblCCN;
		private Label lblVendorName;
		private Label lblVendorCode;
		private Label lblOrderNo;
		private TextBox txtPOOrderNo;
		private C1TrueDBGrid dgrdData;
		private TextBox txtTotalTaxAmount;
		private Label lblTotalTaxAmount;
		private C1NumericEdit txtDistributionAmount;

		#endregion

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private Container components = null;

		#region Constants

		private const string THIS = "PCSProcurement.Purchase.POAdditionCharges";
		private const int DECIMAL_ROUND = 2;
		private const int VAT_NUMBER = 100;
		private const string OPEN_QUOTE = "(";
		private const string CLOSE_QUOTE = ")";
		private const string OR = " OR ";
		private const string IS_NULL = " IS NULL ";
		//private const string GREATER_THAN = ">";
		private const string SMALLER_THAN = "<";
		private const string BY_PRICE = "BYPRICE";
		private const string BY_MANUAL = "BYMANUAL";
		private const string BY_QUANTITY = "BYQUANTITY";
		private const string LINE_COL = "Line";
		
		#endregion

		#region Variables - Properties

		private int intPOMasterID;
		private EnumAction mFormMode;
		private POAdditionChargesBO boPOAddCharge;
		private UtilsBO boUtils;
		private PO_PurchaseOrderMasterVO voPOMaster;
		private PO_PurchaseOrderDetailVO voPODetail;
		private MST_AddChargeVO voAddCharge;
		private MST_ReasonVO voReason;
		private DataSet dstData = new DataSet();
		private DataTable tblPODetails = new DataTable(PO_PurchaseOrderDetailTable.TABLE_NAME);
		private string strSelectedValue;
		private bool blnChargeByQuantity;
        private C1.Win.C1Command.C1DockingTab c1DockingTab1;
        private C1.Win.C1Command.C1DockingTabPage c1DockingTabPage1;
		private System.Windows.Forms.TextBox txtCellValue;
		
		public EnumAction FormMode
		{
			get { return mFormMode; }
			set { mFormMode = value; }
		}

		#endregion

		//**************************************************************************              
		///    <Description>
		///       Default constructor
		///    </Description>
		///    <Inputs>
		///        
		///    </Inputs>
		///    <Outputs>
		///    
		///    </Outputs>
		///    <Returns>
		///    
		///    </Returns>
		///    <Authors>
		///       DungLA
		///    </Authors>
		///    <History>
		///    07-Mar-2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		public POAdditionCharges()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		//**************************************************************************              
		///    <Description>
		///       Parameterise constructor
		///    </Description>
		///    <Inputs>
		///        int: POMasterID
		///    </Inputs>
		///    <Outputs>
		///    
		///    </Outputs>
		///    <Returns>
		///    
		///    </Returns>
		///    <Authors>
		///       DungLA
		///    </Authors>
		///    <History>
		///    17-Feb-2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		public POAdditionCharges(int pintPOMasterID)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			intPOMasterID = pintPOMasterID;
		}
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(POAdditionCharges));
            this.lblTotalAmount = new System.Windows.Forms.Label();
            this.txtTotalAmount = new System.Windows.Forms.TextBox();
            this.txtTotalTaxAmount = new System.Windows.Forms.TextBox();
            this.lblTotalTaxAmount = new System.Windows.Forms.Label();
            this.txtTotalVATAmount = new System.Windows.Forms.TextBox();
            this.lblTotalVATAmount = new System.Windows.Forms.Label();
            this.grbDistribution = new System.Windows.Forms.GroupBox();
            this.txtDistributionAmount = new C1.Win.C1Input.C1NumericEdit();
            this.radByManual = new System.Windows.Forms.RadioButton();
            this.radByPrice = new System.Windows.Forms.RadioButton();
            this.radByQuantity = new System.Windows.Forms.RadioButton();
            this.btnToDistribute = new System.Windows.Forms.Button();
            this.lblDistributionAmount = new System.Windows.Forms.Label();
            this.lblTransDate = new System.Windows.Forms.Label();
            this.lblCCN = new System.Windows.Forms.Label();
            this.lblVendorName = new System.Windows.Forms.Label();
            this.txtVendorCode = new System.Windows.Forms.TextBox();
            this.lblVendorCode = new System.Windows.Forms.Label();
            this.txtPOOrderNo = new System.Windows.Forms.TextBox();
            this.lblOrderNo = new System.Windows.Forms.Label();
            this.btnDelete = new System.Windows.Forms.Button();
            this.btnClose = new System.Windows.Forms.Button();
            this.btnHelp = new System.Windows.Forms.Button();
            this.btnSave = new System.Windows.Forms.Button();
            this.dgrdData = new C1.Win.C1TrueDBGrid.C1TrueDBGrid();
            this.txtTransDate = new System.Windows.Forms.TextBox();
            this.txtCCN = new System.Windows.Forms.TextBox();
            this.txtVendorName = new System.Windows.Forms.TextBox();
            this.txtCellValue = new System.Windows.Forms.TextBox();
            this.c1DockingTab1 = new C1.Win.C1Command.C1DockingTab();
            this.c1DockingTabPage1 = new C1.Win.C1Command.C1DockingTabPage();
            this.grbDistribution.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.txtDistributionAmount)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.dgrdData)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.c1DockingTab1)).BeginInit();
            this.c1DockingTab1.SuspendLayout();
            this.SuspendLayout();
            // 
            // lblTotalAmount
            // 
            resources.ApplyResources(this.lblTotalAmount, "lblTotalAmount");
            this.lblTotalAmount.Name = "lblTotalAmount";
            // 
            // txtTotalAmount
            // 
            resources.ApplyResources(this.txtTotalAmount, "txtTotalAmount");
            this.txtTotalAmount.Name = "txtTotalAmount";
            // 
            // txtTotalTaxAmount
            // 
            resources.ApplyResources(this.txtTotalTaxAmount, "txtTotalTaxAmount");
            this.txtTotalTaxAmount.Name = "txtTotalTaxAmount";
            // 
            // lblTotalTaxAmount
            // 
            resources.ApplyResources(this.lblTotalTaxAmount, "lblTotalTaxAmount");
            this.lblTotalTaxAmount.Name = "lblTotalTaxAmount";
            // 
            // txtTotalVATAmount
            // 
            resources.ApplyResources(this.txtTotalVATAmount, "txtTotalVATAmount");
            this.txtTotalVATAmount.Name = "txtTotalVATAmount";
            // 
            // lblTotalVATAmount
            // 
            resources.ApplyResources(this.lblTotalVATAmount, "lblTotalVATAmount");
            this.lblTotalVATAmount.Name = "lblTotalVATAmount";
            // 
            // grbDistribution
            // 
            resources.ApplyResources(this.grbDistribution, "grbDistribution");
            this.grbDistribution.Controls.Add(this.txtDistributionAmount);
            this.grbDistribution.Controls.Add(this.radByManual);
            this.grbDistribution.Controls.Add(this.radByPrice);
            this.grbDistribution.Controls.Add(this.radByQuantity);
            this.grbDistribution.Controls.Add(this.btnToDistribute);
            this.grbDistribution.Controls.Add(this.lblDistributionAmount);
            this.grbDistribution.Name = "grbDistribution";
            this.grbDistribution.TabStop = false;
            // 
            // txtDistributionAmount
            // 
            resources.ApplyResources(this.txtDistributionAmount, "txtDistributionAmount");
            this.txtDistributionAmount.DisableOnNoData = false;
            this.txtDistributionAmount.DisplayFormat.Inherit = ((C1.Win.C1Input.FormatInfoInheritFlags)(resources.GetObject("txtDistributionAmount.DisplayFormat.Inherit")));
            this.txtDistributionAmount.EditFormat.Inherit = ((C1.Win.C1Input.FormatInfoInheritFlags)(resources.GetObject("txtDistributionAmount.EditFormat.Inherit")));
            this.txtDistributionAmount.Name = "txtDistributionAmount";
            this.txtDistributionAmount.ParseInfo.Inherit = ((C1.Win.C1Input.ParseInfoInheritFlags)(resources.GetObject("txtDistributionAmount.ParseInfo.Inherit")));
            this.txtDistributionAmount.PreValidation.Inherit = ((C1.Win.C1Input.PreValidationInheritFlags)(resources.GetObject("txtDistributionAmount.PreValidation.Inherit")));
            // 
            // radByManual
            // 
            resources.ApplyResources(this.radByManual, "radByManual");
            this.radByManual.Name = "radByManual";
            this.radByManual.CheckedChanged += new System.EventHandler(this.radManual_CheckedChanged);
            // 
            // radByPrice
            // 
            resources.ApplyResources(this.radByPrice, "radByPrice");
            this.radByPrice.Name = "radByPrice";
            this.radByPrice.CheckedChanged += new System.EventHandler(this.radByPrice_CheckedChanged);
            // 
            // radByQuantity
            // 
            resources.ApplyResources(this.radByQuantity, "radByQuantity");
            this.radByQuantity.Name = "radByQuantity";
            this.radByQuantity.CheckedChanged += new System.EventHandler(this.radByQuantity_CheckedChanged);
            // 
            // btnToDistribute
            // 
            resources.ApplyResources(this.btnToDistribute, "btnToDistribute");
            this.btnToDistribute.Name = "btnToDistribute";
            this.btnToDistribute.Click += new System.EventHandler(this.btnToDistribute_Click);
            // 
            // lblDistributionAmount
            // 
            resources.ApplyResources(this.lblDistributionAmount, "lblDistributionAmount");
            this.lblDistributionAmount.Name = "lblDistributionAmount";
            // 
            // lblTransDate
            // 
            this.lblTransDate.ForeColor = System.Drawing.SystemColors.ControlText;
            resources.ApplyResources(this.lblTransDate, "lblTransDate");
            this.lblTransDate.Name = "lblTransDate";
            // 
            // lblCCN
            // 
            this.lblCCN.ForeColor = System.Drawing.SystemColors.ControlText;
            resources.ApplyResources(this.lblCCN, "lblCCN");
            this.lblCCN.Name = "lblCCN";
            // 
            // lblVendorName
            // 
            this.lblVendorName.ForeColor = System.Drawing.SystemColors.ControlText;
            resources.ApplyResources(this.lblVendorName, "lblVendorName");
            this.lblVendorName.Name = "lblVendorName";
            // 
            // txtVendorCode
            // 
            resources.ApplyResources(this.txtVendorCode, "txtVendorCode");
            this.txtVendorCode.Name = "txtVendorCode";
            // 
            // lblVendorCode
            // 
            this.lblVendorCode.ForeColor = System.Drawing.SystemColors.ControlText;
            resources.ApplyResources(this.lblVendorCode, "lblVendorCode");
            this.lblVendorCode.Name = "lblVendorCode";
            // 
            // txtPOOrderNo
            // 
            resources.ApplyResources(this.txtPOOrderNo, "txtPOOrderNo");
            this.txtPOOrderNo.Name = "txtPOOrderNo";
            // 
            // lblOrderNo
            // 
            this.lblOrderNo.ForeColor = System.Drawing.SystemColors.ControlText;
            resources.ApplyResources(this.lblOrderNo, "lblOrderNo");
            this.lblOrderNo.Name = "lblOrderNo";
            // 
            // btnDelete
            // 
            resources.ApplyResources(this.btnDelete, "btnDelete");
            this.btnDelete.Name = "btnDelete";
            this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
            // 
            // btnClose
            // 
            resources.ApplyResources(this.btnClose, "btnClose");
            this.btnClose.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.btnClose.Name = "btnClose";
            this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
            // 
            // btnHelp
            // 
            resources.ApplyResources(this.btnHelp, "btnHelp");
            this.btnHelp.Name = "btnHelp";
            this.btnHelp.Click += new System.EventHandler(this.btnHelp_Click);
            // 
            // btnSave
            // 
            resources.ApplyResources(this.btnSave, "btnSave");
            this.btnSave.Name = "btnSave";
            this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
            // 
            // dgrdData
            // 
            resources.ApplyResources(this.dgrdData, "dgrdData");
            this.dgrdData.Images.Add(((System.Drawing.Image)(resources.GetObject("dgrdData.Images"))));
            this.dgrdData.Name = "dgrdData";
            this.dgrdData.PreviewInfo.Location = new System.Drawing.Point(0, 0);
            this.dgrdData.PreviewInfo.Size = new System.Drawing.Size(0, 0);
            this.dgrdData.PreviewInfo.ZoomFactor = 75D;
            this.dgrdData.PrintInfo.PageSettings = ((System.Drawing.Printing.PageSettings)(resources.GetObject("dgrdData.PrintInfo.PageSettings")));
            this.dgrdData.AfterColUpdate += new C1.Win.C1TrueDBGrid.ColEventHandler(this.dgrdData_AfterColUpdate);
            this.dgrdData.AfterDelete += new System.EventHandler(this.dgrdData_AfterDelete);
            this.dgrdData.BeforeColUpdate += new C1.Win.C1TrueDBGrid.BeforeColUpdateEventHandler(this.dgrdData_BeforeColUpdate);
            this.dgrdData.BeforeDelete += new C1.Win.C1TrueDBGrid.CancelEventHandler(this.dgrdData_BeforeDelete);
            this.dgrdData.RowColChange += new C1.Win.C1TrueDBGrid.RowColChangeEventHandler(this.dgrdData_RowColChange);
            this.dgrdData.BeforeColEdit += new C1.Win.C1TrueDBGrid.BeforeColEditEventHandler(this.dgrdData_BeforeColEdit);
            this.dgrdData.ButtonClick += new C1.Win.C1TrueDBGrid.ColEventHandler(this.dgrdData_ButtonClick);
            this.dgrdData.KeyDown += new System.Windows.Forms.KeyEventHandler(this.dgrdData_KeyDown);
            this.dgrdData.PropBag = resources.GetString("dgrdData.PropBag");
            // 
            // txtTransDate
            // 
            resources.ApplyResources(this.txtTransDate, "txtTransDate");
            this.txtTransDate.Name = "txtTransDate";
            // 
            // txtCCN
            // 
            resources.ApplyResources(this.txtCCN, "txtCCN");
            this.txtCCN.Name = "txtCCN";
            // 
            // txtVendorName
            // 
            resources.ApplyResources(this.txtVendorName, "txtVendorName");
            this.txtVendorName.Name = "txtVendorName";
            // 
            // txtCellValue
            // 
            this.txtCellValue.BorderStyle = System.Windows.Forms.BorderStyle.None;
            resources.ApplyResources(this.txtCellValue, "txtCellValue");
            this.txtCellValue.Name = "txtCellValue";
            // 
            // c1DockingTab1
            // 
            this.c1DockingTab1.Controls.Add(this.c1DockingTabPage1);
            resources.ApplyResources(this.c1DockingTab1, "c1DockingTab1");
            this.c1DockingTab1.Name = "c1DockingTab1";
            this.c1DockingTab1.TabsSpacing = 0;
            this.c1DockingTab1.VisualStyleBase = C1.Win.C1Command.VisualStyle.OfficeXP;
            // 
            // c1DockingTabPage1
            // 
            resources.ApplyResources(this.c1DockingTabPage1, "c1DockingTabPage1");
            this.c1DockingTabPage1.Name = "c1DockingTabPage1";
            // 
            // POAdditionCharges
            // 
            resources.ApplyResources(this, "$this");
            this.CancelButton = this.btnClose;
            this.Controls.Add(this.c1DockingTab1);
            this.Controls.Add(this.lblTotalVATAmount);
            this.Controls.Add(this.txtVendorName);
            this.Controls.Add(this.txtCCN);
            this.Controls.Add(this.txtTransDate);
            this.Controls.Add(this.txtTotalAmount);
            this.Controls.Add(this.txtTotalTaxAmount);
            this.Controls.Add(this.txtTotalVATAmount);
            this.Controls.Add(this.txtVendorCode);
            this.Controls.Add(this.txtPOOrderNo);
            this.Controls.Add(this.dgrdData);
            this.Controls.Add(this.txtCellValue);
            this.Controls.Add(this.grbDistribution);
            this.Controls.Add(this.lblTransDate);
            this.Controls.Add(this.lblCCN);
            this.Controls.Add(this.lblVendorName);
            this.Controls.Add(this.lblVendorCode);
            this.Controls.Add(this.lblOrderNo);
            this.Controls.Add(this.btnDelete);
            this.Controls.Add(this.btnClose);
            this.Controls.Add(this.btnHelp);
            this.Controls.Add(this.btnSave);
            this.Controls.Add(this.lblTotalAmount);
            this.Controls.Add(this.lblTotalTaxAmount);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.KeyPreview = true;
            this.Name = "POAdditionCharges";
            this.Closing += new System.ComponentModel.CancelEventHandler(this.POAdditionCharges_Closing);
            this.Load += new System.EventHandler(this.POAdditionCharges_Load);
            this.grbDistribution.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.txtDistributionAmount)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.dgrdData)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.c1DockingTab1)).EndInit();
            this.c1DockingTab1.ResumeLayout(false);
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

		#region Form events
		//**************************************************************************              
		///    <Description>
		///       Call SOCommon.GetSOByMasterID to get SO dataset 
		///       then fill in the following items
		///       - CCN
		///       - Order Number
		///       - OrderDate
		///       - Customer Code 
		///       - Customer Name
		///       - Line, SO Line, Addition Charge, Reason, Quantity, Unit of Price
		///       - Calculate :Total VAT Amount, Total Amount, Total NetAmount
		///       - If the all of product have same unit of measurement 
		///       then set radByQuantity = True
		///       - Else radByPrice = True
		///    </Description>
		///    <Inputs>
		///        
		///    </Inputs>
		///    <Outputs>
		///    
		///    </Outputs>
		///    <Returns>
		///    
		///    </Returns>
		///    <Authors>
		///       DungLA
		///    </Authors>
		///    <History>
		///    07-Mar-2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		private void POAdditionCharges_Load(object sender, EventArgs e)
		{
			const string METHOD_NAME = THIS + ".POAdditionCharges_Load()";
			try
			{
				#region Apply security
				//Set authorization for user
				Security objSecurity = new Security();
				this.Name = THIS;
				if(objSecurity.SetRightForUserOnForm(this, SystemProperty.UserName) == 0)
				{
					Close();
					PCSMessageBox.Show(ErrorCode.MESSAGE_YOU_HAVE_NO_RIGHT_TO_VIEW, MessageBoxIcon.Warning);
					return;
				}
				#endregion

				// initial value
				voPOMaster = new PO_PurchaseOrderMasterVO();
				voPODetail = new PO_PurchaseOrderDetailVO();
				voAddCharge = new MST_AddChargeVO();
				voReason = new MST_ReasonVO();
				boPOAddCharge = new POAdditionChargesBO();
				boUtils = new UtilsBO();

				// format distribution amount text box
				txtDistributionAmount.FormatType = FormatTypeEnum.CustomFormat;
				txtDistributionAmount.CustomFormat = Constants.DECIMAL_NUMBERFORMAT;
				
				// get POMasterVO object
				voPOMaster = (PO_PurchaseOrderMasterVO)boPOAddCharge.GetPOMasterVO(intPOMasterID);
				bool blnIsCharged = boPOAddCharge.AlreadyCharged(intPOMasterID);
				// if already charged, get old value
				if (blnIsCharged)
				{
					dstData = boPOAddCharge.GetAdditionalChargeByPOMasterID(intPOMasterID);
					decimal decDistributionAmount = 0;
					// re-calculate lastest distribution amount
					foreach (DataRow drowData in dstData.Tables[0].Rows)
					{
						decDistributionAmount += decimal.Parse(drowData[PO_AdditionChargesTable.AMOUNT_FLD].ToString());
					}
					txtDistributionAmount.Value = decDistributionAmount.ToString(Constants.CELL_NUMBER_FORMAT);
				}
				else // else get new value
					dstData = boPOAddCharge.GetDataByPOMasterID(intPOMasterID);
				// assign default value to PurchaseOrderMasterID column
				dstData.Tables[0].Columns[PO_AdditionChargesTable.PURCHASEORDERMASTERID_FLD].DefaultValue = intPOMasterID;
				// get all PODetail by Master
				tblPODetails = boPOAddCharge.ListPODetailByPOMaster(intPOMasterID);
				
				// get CCN code fill to form
				txtCCN.Text = boUtils.GetCCNCodeFromID(voPOMaster.CCNID);
				txtPOOrderNo.Text = voPOMaster.Code;
				txtTransDate.Text = voPOMaster.OrderDate.ToString(Constants.DATETIME_FORMAT);
				// get customer information
				MST_Party voParty = new MST_Party();
				voParty = new PartyBO().GetParty(voPOMaster.PartyID,string.Empty);
				// fill to label
				txtVendorCode.Text = voParty.Code;
				txtVendorName.Text = voParty.Name;
				// bind data source for the true db grid
				BindDataToGrid();
				// update Total Amount, Total NET Amount, Total VAT Amount
				UpdateTotalAmount();
				// if all of product have the same UM then set radByQuantity = true
				blnChargeByQuantity = boPOAddCharge.IsChargeByQuantity(intPOMasterID);
				if (blnChargeByQuantity)
				{
					// charge by quantity
					radByQuantity.Checked = true;
					//dgrdData.Splits[0].DisplayColumns[PO_AdditionChargesTable.UNITPRICE_FLD].Locked = false;
					//dgrdData.Splits[0].DisplayColumns[PO_AdditionChargesTable.QUANTITY_FLD].Locked = false;
				}
				else
				{
					radByPrice.Checked = true;
				}
				txtDistributionAmount.Focus();
				#region Set Textbox to read-only
				txtPOOrderNo.ReadOnly = true;
				txtTransDate.ReadOnly = true;
				txtVendorCode.ReadOnly = true;
				txtVendorName.ReadOnly = true;
				txtCCN.ReadOnly = true;
				txtTotalAmount.ReadOnly = true;
				txtTotalTaxAmount.ReadOnly = true;
				txtTotalVATAmount.ReadOnly = true;
				#endregion
				AssignEvent(this);
			}
			catch (PCSException ex)
			{
				// displays the error message.
				PCSMessageBox.Show(ex.mCode, MessageBoxButtons.OK, MessageBoxIcon.Error);
				// log message.
				try
				{
					Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
				}
				catch
				{
					PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}
			catch (Exception ex)
			{
				// displays the error message.
				PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
				// log message.
				try
				{
					Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
				}
				catch
				{
					PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}
		}
		//**************************************************************************              
		///    <Description>
		///       
		///    </Description>
		///    <Inputs>
		///        
		///    </Inputs>
		///    <Outputs>
		///    
		///    </Outputs>
		///    <Returns>
		///    
		///    </Returns>
		///    <Authors>
		///       DungLA
		///    </Authors>
		///    <History>
		///    07-Mar-2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		private void btnClose_Click(object sender, EventArgs e)
		{
			Close();
		}

		//**************************************************************************              
		///    <Description>
		///       - Save into Database and show success message
		///    </Description>
		///    <Inputs>
		///        
		///    </Inputs>
		///    <Outputs>
		///    
		///    </Outputs>
		///    <Returns>
		///    
		///    </Returns>
		///    <Authors>
		///       DungLA
		///    </Authors>
		///    <History>
		///    07-Mar-2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		private void btnSave_Click(object sender, EventArgs e)
		{
			const string METHOD_NAME = THIS + ".btnSave_Click()";
			try
			{
				// check mandatory field
				int intTotalRows = dgrdData.RowCount;
				if (mFormMode != EnumAction.Edit)
				{
					if (intTotalRows <= 0)
						return;
					for (int i = 0; i < intTotalRows; i++)
					{
						// po line
						try
						{
							int.Parse(dgrdData[i, PO_PurchaseOrderDetailTable.TABLE_NAME + PO_PurchaseOrderDetailTable.LINE_FLD].ToString());
						}
						catch
						{
							PCSMessageBox.Show(ErrorCode.MANDATORY_INVALID, MessageBoxButtons.OK, MessageBoxIcon.Error);
							dgrdData.Row = i;
							dgrdData.Col = dgrdData.Columns.IndexOf(dgrdData.Columns[PO_PurchaseOrderDetailTable.TABLE_NAME + PO_PurchaseOrderDetailTable.LINE_FLD]);
							return;
						}
						// addition charge
						try
						{
							int.Parse(dgrdData[i, PO_AdditionChargesTable.ADDCHARGEID_FLD].ToString());
						}
						catch
						{
							PCSMessageBox.Show(ErrorCode.MANDATORY_INVALID, MessageBoxButtons.OK, MessageBoxIcon.Error);
							dgrdData.Row = i;
							dgrdData.Col = dgrdData.Columns.IndexOf(dgrdData.Columns[MST_AddChargeTable.TABLE_NAME + MST_AddChargeTable.CODE_FLD]);
							return;
						}
						// charge quantity
						try
						{
							decimal decQuantity = decimal.Parse(dgrdData[i, PO_AdditionChargesTable.QUANTITY_FLD].ToString());
							decimal decUnitPrice = decimal.Parse(dgrdData[i, PO_AdditionChargesTable.UNITPRICE_FLD].ToString());
							decimal decAmount = decimal.Parse(dgrdData[i, PO_AdditionChargesTable.AMOUNT_FLD].ToString());
							if ((decQuantity < 0) || (decUnitPrice < 0) || (decAmount < 0))
							{
								PCSMessageBox.Show(ErrorCode.MESSAGE_CANNOT_INPUT_NEGATIVE_NUMBER, MessageBoxButtons.OK, MessageBoxIcon.Error);
								dgrdData.Row = i;
								dgrdData.Col = dgrdData.Columns.IndexOf(dgrdData.Columns[PO_AdditionChargesTable.QUANTITY_FLD]);
								return;
							}
						}
						catch
						{
							PCSMessageBox.Show(ErrorCode.MANDATORY_INVALID, MessageBoxButtons.OK, MessageBoxIcon.Error);
							dgrdData.Row = i;
							dgrdData.Col = dgrdData.Columns.IndexOf(dgrdData.Columns[PO_AdditionChargesTable.QUANTITY_FLD]);
							return;
						}
					}
					// if Base Amount is greater than Distribution Amount
					// display error message
					try
					{
						if (decimal.Parse(txtTotalTaxAmount.Text.Trim()) > decimal.Parse(txtDistributionAmount.Value.ToString()))
						{
							//PCSMessageBox.Show(ErrorCode.MESSAGE_BASE_AMOUNT_GREATER_THAN_DISTRIBUTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
							MessageBox.Show("Base Amount cannot greater than Distribution Amount");
							return;
						}
					}
					catch{}
				}
				if (SaveData())
				{
					// display success message
					PCSMessageBox.Show(ErrorCode.MESSAGE_AFTER_SAVE_DATA, MessageBoxButtons.OK, MessageBoxIcon.Information);
					// get new data from database
					dstData = boPOAddCharge.GetAdditionalChargeByPOMasterID(intPOMasterID);
					// rebind to grid
					BindDataToGrid();
				}
				else
				{
					// display error message
					PCSMessageBox.Show(ErrorCode.MESSAGE_CANNOT_SAVE_TO_DB, MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}
			catch (PCSException ex)
			{
				// displays the error message.
				PCSMessageBox.Show(ex.mCode, MessageBoxButtons.OK, MessageBoxIcon.Error);
				// log message.
				try
				{
					Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
				}
				catch
				{
					PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}
			catch (Exception ex)
			{
				// displays the error message.
				PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
				// log message.
				try
				{
					Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
				}
				catch
				{
					PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}
		}

		//**************************************************************************              
		///    <Description>
		///       
		///    </Description>
		///    <Inputs>
		///        
		///    </Inputs>
		///    <Outputs>
		///    
		///    </Outputs>
		///    <Returns>
		///    
		///    </Returns>
		///    <Authors>
		///       DungLA
		///    </Authors>
		///    <History>
		///    07-Mar-2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		private void btnDelete_Click(object sender, EventArgs e)
		{
			const string METHOD_NAME = THIS + ".btnDelete_Click()";
			if (dgrdData.RowCount == 0)
			{
				return;
			}
			// ask user to confirm delete
			DialogResult dlgResult = PCSMessageBox.Show(ErrorCode.MESSAGE_DELETE_RECORD, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
			if (dlgResult == DialogResult.Yes)
			{
				try
				{
					int intRowCount = dgrdData.RowCount;
					for (int i = intRowCount - 1; i >= 0; i--)
					{
						dstData.Tables[0].Rows[i].Delete();
					}
					dgrdData.UpdateData();
					mFormMode = EnumAction.Edit;
					// turn allow addnew
					dgrdData.AllowAddNew = true;
					txtTotalAmount.Text = decimal.Zero.ToString();
					txtTotalTaxAmount.Text = decimal.Zero.ToString();
					txtTotalVATAmount.Text = decimal.Zero.ToString();
				}
				catch (Exception ex)
				{
					PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
					// log message.
					try
					{
						Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
					}
					catch
					{
						PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
					}
				}
			}
		}

		//**************************************************************************              
		///    <Description>
		///       
		///    </Description>
		///    <Inputs>
		///        
		///    </Inputs>
		///    <Outputs>
		///    
		///    </Outputs>
		///    <Returns>
		///    
		///    </Returns>
		///    <Authors>
		///       DungLA
		///    </Authors>
		///    <History>
		///    07-Mar-2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		private void btnHelp_Click(object sender, EventArgs e)
		{
		
		}

		//**************************************************************************              
		///    <Description>
		///       - Fill data for Line cell = last cell value + 1
		///       - Fill SO Line = Line number in sales order
		///       - If ByQuantity = True, 
		///				fill Amount = Quantity/(total Quantity) * Distribution Amount 
		///       - If ByPrice = True, 
		///				fill Amount = Unit Price/(total Unit Price) * Distribution Amount
		///       - Fill VAT Amount = Amount * 0.1 * VAT
		///       - Fill Total Amount = Amount + VAT Amount
		///       - Update Total Amount, Total VAT Amount, Total Net Amount
		///    </Description>
		///    <Inputs>
		///        
		///    </Inputs>
		///    <Outputs>
		///    
		///    </Outputs>
		///    <Returns>
		///    
		///    </Returns>
		///    <Authors>
		///       DungLA
		///    </Authors>
		///    <History>
		///    07-Mar-2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		private void btnToDistribute_Click(object sender, EventArgs e)
		{
			const string METHOD_NAME = THIS + ".btnToDistribute_Click()";
			try
			{
				if (radByPrice.Checked)
					UpdateAmount(BY_PRICE, false);
				else if (radByQuantity.Checked)
					UpdateAmount(BY_QUANTITY, false);
			}
			catch (PCSException ex)
			{
				// displays the error message.
				PCSMessageBox.Show(ex.mCode, MessageBoxButtons.OK, MessageBoxIcon.Error);
				// log message.
				try
				{
					Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
				}
				catch
				{
					PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}
			catch (Exception ex)
			{
				// displays the error message.
				PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
				// log message.
				try
				{
					Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
				}
				catch
				{
					PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}
		}

		//**************************************************************************              
		///    <Description>
		///       
		///    </Description>
		///    <Inputs>
		///        
		///    </Inputs>
		///    <Outputs>
		///    
		///    </Outputs>
		///    <Returns>
		///    
		///    </Returns>
		///    <Authors>
		///       DungLA
		///    </Authors>
		///    <History>
		///    07-Mar-2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		private void radByPrice_CheckedChanged(object sender, EventArgs e)
		{
//			if (blnChargeByQuantity)
//			{
//				PCSMessageBox.Show(ErrorCode.MESSAGE_COULDNOT_CHARGE_BY_PRICE, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
//				if (blnChargeManual)
//					radByManual.Checked = true;
//				else
//					radByQuantity.Checked = true;
//				return;
//			}
//			blnChargeManual = false;
			// enable btnToDistribute
			btnToDistribute.Enabled = true;
		}
		//**************************************************************************              
		///    <Description>
		///       
		///    </Description>
		///    <Inputs>
		///        
		///    </Inputs>
		///    <Outputs>
		///    
		///    </Outputs>
		///    <Returns>
		///    
		///    </Returns>
		///    <Authors>
		///       DungLA
		///    </Authors>
		///    <History>
		///    07-Mar-2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		private void radByQuantity_CheckedChanged(object sender, EventArgs e)
		{
//			if (!blnChargeByQuantity)
//			{
//				PCSMessageBox.Show(ErrorCode.MESSAGE_COULDNOT_CHARGE_BY_QUANTITY, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
//				if (blnChargeManual)
//					radByManual.Checked = true;
//				else
//					radByPrice.Checked = true;
//				return;
//			}
//			blnChargeManual = false;
			// enable btnToDistribute
			btnToDistribute.Enabled = true;
		}
		//**************************************************************************              
		///    <Description>
		///       - Clear all rows in dgrdData
		///       - Disable btnToDistribute
		///    </Description>
		///    <Inputs>
		///        
		///    </Inputs>
		///    <Outputs>
		///    
		///    </Outputs>
		///    <Returns>
		///    
		///    </Returns>
		///    <Authors>
		///       DungLA
		///    </Authors>
		///    <History>
		///    07-Mar-2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		private void radManual_CheckedChanged(object sender, EventArgs e)
		{
			const string METHOD_NAME = THIS + ".radManual_CheckedChanged()";
			try
			{
				// clear all rows in grid data
				//dstData.Tables[0].Clear();
				// allow grid to add new row
				//dgrdData.AllowAddNew = true;
				//txtTotalAmount.Text = decimal.Zero.ToString();
				//txtTotalTaxAmount.Text = decimal.Zero.ToString();
				//txtTotalVATAmount.Text = decimal.Zero.ToString();
				// disable btnToDistribute
				btnToDistribute.Enabled = false;
				//blnChargeManual = true;
			}
			catch (PCSException ex)
			{
				// displays the error message.
				PCSMessageBox.Show(ex.mCode, MessageBoxButtons.OK, MessageBoxIcon.Error);
				// log message.
				try
				{
					Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
				}
				catch
				{
					PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}
			catch (Exception ex)
			{
				// displays the error message.
				PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
				// log message.
				try
				{
					Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
				}
				catch
				{
					PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}
		}
		//**************************************************************************              
		///    <Description>
		///     fill quantity, unit price when user select an item from SO Line
		///    </Description>
		///    <Inputs>
		///        
		///    </Inputs>
		///    <Outputs>
		///    
		///    </Outputs>
		///    <Returns>
		///    
		///    </Returns>
		///    <Authors>
		///       DungLA
		///    </Authors>
		///    <History>
		///    22-Feb-2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		private void dgrdData_AfterColUpdate(object sender, ColEventArgs e)
		{
			const string METHOD_NAME = THIS + ".dgrdData_AfterColUpdate()";
			try
			{
				string strSelectedColumn = dgrdData.Columns[dgrdData.Col].DataField;
				decimal decSelectedValue = 0;
				if (!strSelectedColumn.Equals(PO_PurchaseOrderDetailTable.TABLE_NAME + PO_PurchaseOrderDetailTable.LINE_FLD) && !strSelectedColumn.Equals(MST_AddChargeTable.TABLE_NAME + MST_AddChargeTable.CODE_FLD)
					&& !strSelectedColumn.Equals(MST_ReasonTable.TABLE_NAME + MST_ReasonTable.CODE_FLD))
				{
					try
					{
						decSelectedValue = decimal.Parse(dgrdData[dgrdData.Row, dgrdData.Col].ToString().Trim());
						if (decSelectedValue < 0)
						{
							PCSMessageBox.Show(ErrorCode.MESSAGE_CANNOT_INPUT_NEGATIVE_NUMBER, MessageBoxButtons.OK, MessageBoxIcon.Error);
							dgrdData.Select();
							dgrdData[dgrdData.Row, dgrdData.Col] = strSelectedValue;
							return;
						}
					}
					catch
					{
						PCSMessageBox.Show(ErrorCode.MESSAGE_INVALID_NUMERIC, MessageBoxButtons.OK, MessageBoxIcon.Error);
						dgrdData.Focus();
						dgrdData.Select();
						dgrdData[dgrdData.Row, dgrdData.Col] = strSelectedValue;
						return;
					}
				}
				switch (strSelectedColumn)
				{
					case PO_AdditionChargesTable.QUANTITY_FLD:
					case PO_AdditionChargesTable.UNITPRICE_FLD:
						// update amount on grid
						if (radByPrice.Checked)
							UpdateAmount(BY_PRICE, true);
						else if (radByQuantity.Checked)
							UpdateAmount(BY_QUANTITY, true);
						else if (radByManual.Checked)
							UpdateAmount(BY_MANUAL, true);
						break;
					// amount
					case PO_AdditionChargesTable.AMOUNT_FLD:
						// get AddChargeID
						if (dgrdData[dgrdData.Row, PO_AdditionChargesTable.ADDCHARGEID_FLD].ToString() != string.Empty &&
							(dgrdData[dgrdData.Row, PO_AdditionChargesTable.ADDCHARGEID_FLD] != null))
						{
							// get addition charge vo
							voAddCharge = (MST_AddChargeVO) boPOAddCharge.GetAddCharge(int.Parse(dgrdData[dgrdData.Row, PO_AdditionChargesTable.ADDCHARGEID_FLD].ToString()));
						}
						else
						{
							PCSMessageBox.Show(ErrorCode.MANDATORY_INVALID, MessageBoxButtons.OK, MessageBoxIcon.Error);
							dgrdData.Focus();
							dgrdData.Select();
							dgrdData[dgrdData.Row, dgrdData.Col] = strSelectedValue;
							return;
						}
						//decAmount
						dgrdData[dgrdData.Row, PO_AdditionChargesTable.VATAMOUNT_FLD] = Math.Round(decSelectedValue * voAddCharge.VAT / VAT_NUMBER, DECIMAL_ROUND);
						dgrdData[dgrdData.Row, PO_AdditionChargesTable.TOTALAMOUNT_FLD] = Math.Round(decSelectedValue + decimal.Parse(dgrdData.Columns[PO_AdditionChargesTable.VATAMOUNT_FLD].ToString()), DECIMAL_ROUND);
						// update TotalAmount, TotalVATAmount, TotalNETAmount;
						UpdateTotalAmount();
						break;
					// VAT amount
					case PO_AdditionChargesTable.VATAMOUNT_FLD:
						dgrdData[dgrdData.Row, PO_AdditionChargesTable.TOTALAMOUNT_FLD] = Math.Round(decSelectedValue + decimal.Parse(dgrdData.Columns[PO_AdditionChargesTable.AMOUNT_FLD].ToString()), DECIMAL_ROUND);
						// update TotalAmount, TotalVATAmount, TotalNETAmount;
						UpdateTotalAmount();
						break;
					// total amount
					case PO_AdditionChargesTable.TOTALAMOUNT_FLD:
						// update TotalAmount, TotalVATAmount, TotalNETAmount;
						UpdateTotalAmount();
						break;
				}
			}
			catch (PCSException ex)
			{
				dgrdData[dgrdData.Row, dgrdData.Col] = strSelectedValue;
				// displays the error message.
				PCSMessageBox.Show(ex.mCode, MessageBoxButtons.OK, MessageBoxIcon.Error);
				// log message.
				try
				{
					Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
				}
				catch
				{
					PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}
			catch (Exception ex)
			{
				dgrdData[dgrdData.Row, dgrdData.Col] = strSelectedValue;
				// displays the error message.
				PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
				// log message.
				try
				{
					Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
				}
				catch
				{
					PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}
		}
		//**************************************************************************              
		///    <Description>
		///     get the selected value and store to a variable in order to compare later
		///    </Description>
		///    <Inputs>
		///        
		///    </Inputs>
		///    <Outputs>
		///    
		///    </Outputs>
		///    <Returns>
		///    
		///    </Returns>
		///    <Authors>
		///       DungLA
		///    </Authors>
		///    <History>
		///    22-Feb-2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		private void dgrdData_ButtonClick(object sender, ColEventArgs e)
		{
			const string METHOD_NAME = THIS + ".dgrdData_AfterColUpdate()";
			try
			{
				// if current column is locked then return
				if (dgrdData.Splits[0].DisplayColumns[dgrdData.Col].Locked)
					return;
				FillDataToGrid();
			}
			catch (PCSException ex)
			{
				// displays the error message.
				PCSMessageBox.Show(ex.mCode, MessageBoxButtons.OK, MessageBoxIcon.Error);
				// log message.
				try
				{
					Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
				}
				catch
				{
					PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}
			catch (Exception ex)
			{
				// displays the error message.
				PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
				// log message.
				try
				{
					Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
				}
				catch
				{
					PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}
		}
		//**************************************************************************              
		///    <Description>
		///     get the selected value and store to a variable in order to compare later
		///    </Description>
		///    <Inputs>
		///        
		///    </Inputs>
		///    <Outputs>
		///    
		///    </Outputs>
		///    <Returns>
		///    
		///    </Returns>
		///    <Authors>
		///       DungLA
		///    </Authors>
		///    <History>
		///    22-Feb-2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		private void dgrdData_BeforeColEdit(object sender, BeforeColEditEventArgs e)
		{
			const string METHOD_NAME = THIS + ".dgrdData_BeforeColEdit()";
			try
			{
				// get the selected value and store to a variable in order to compare later
				strSelectedValue = dgrdData[dgrdData.Row, e.ColIndex].ToString();
				// now we need to check if current PO Line is approved or not?
				// get PO Detail of current row
				int intPODetailID = 0;
				try
				{
					intPODetailID = int.Parse(dgrdData[dgrdData.Row, PO_AdditionChargesTable.PURCHASEORDERDETAILID_FLD].ToString());
				}
				catch{}
				if (intPODetailID <= 0)
					return;
				voPODetail = (PO_PurchaseOrderDetailVO)boPOAddCharge.GetPurchaseOrderDetailVO(intPODetailID);
				// if the PODetail of current row has been approved, then unable to change anything
				if (voPODetail.ApproverID > 0)
				{
					e.Cancel = true;
					// lock all editable column
					dgrdData.Splits[0].DisplayColumns[PO_AdditionChargesTable.UNITPRICE_FLD].Locked = true;
					dgrdData.Splits[0].DisplayColumns[PO_AdditionChargesTable.QUANTITY_FLD].Locked = true;
					dgrdData.Splits[0].DisplayColumns[PO_AdditionChargesTable.AMOUNT_FLD].Locked = true;
					dgrdData.Splits[0].DisplayColumns[PO_AdditionChargesTable.VATAMOUNT_FLD].Locked = true;
					// unable to change PO, Addition Charge, Reason
					dgrdData.Splits[0].DisplayColumns[MST_ReasonTable.TABLE_NAME + MST_ReasonTable.CODE_FLD].Button = false;
					dgrdData.Splits[0].DisplayColumns[MST_ReasonTable.TABLE_NAME + MST_ReasonTable.CODE_FLD].Locked = true;
					dgrdData.Splits[0].DisplayColumns[MST_AddChargeTable.TABLE_NAME + MST_AddChargeTable.CODE_FLD].Button = false;
					dgrdData.Splits[0].DisplayColumns[MST_AddChargeTable.TABLE_NAME + MST_AddChargeTable.CODE_FLD].Locked = true;
					dgrdData.Splits[0].DisplayColumns[PO_PurchaseOrderDetailTable.TABLE_NAME + PO_PurchaseOrderDetailTable.LINE_FLD].Button = false;
					dgrdData.Splits[0].DisplayColumns[PO_PurchaseOrderDetailTable.TABLE_NAME + PO_PurchaseOrderDetailTable.LINE_FLD].Locked = true;
				}
				else
				{
					// unlock Unit price and quantity
					dgrdData.Splits[0].DisplayColumns[PO_AdditionChargesTable.UNITPRICE_FLD].Locked = false;
					dgrdData.Splits[0].DisplayColumns[PO_AdditionChargesTable.QUANTITY_FLD].Locked = false;
					dgrdData.Splits[0].DisplayColumns[PO_AdditionChargesTable.AMOUNT_FLD].Locked = false;
					dgrdData.Splits[0].DisplayColumns[PO_AdditionChargesTable.VATAMOUNT_FLD].Locked = false;
					// allow to change PO, Addition Charge, Reason
					dgrdData.Splits[0].DisplayColumns[MST_ReasonTable.TABLE_NAME + MST_ReasonTable.CODE_FLD].Button = true;
					dgrdData.Splits[0].DisplayColumns[MST_ReasonTable.TABLE_NAME + MST_ReasonTable.CODE_FLD].Locked = false;
					dgrdData.Splits[0].DisplayColumns[MST_AddChargeTable.TABLE_NAME + MST_AddChargeTable.CODE_FLD].Button = true;
					dgrdData.Splits[0].DisplayColumns[MST_AddChargeTable.TABLE_NAME + MST_AddChargeTable.CODE_FLD].Locked = false;
					dgrdData.Splits[0].DisplayColumns[PO_PurchaseOrderDetailTable.TABLE_NAME + PO_PurchaseOrderDetailTable.LINE_FLD].Button = true;
					dgrdData.Splits[0].DisplayColumns[PO_PurchaseOrderDetailTable.TABLE_NAME + PO_PurchaseOrderDetailTable.LINE_FLD].Locked = false;
				}
			}
			catch (Exception ex)
			{
				// displays the error message.
				PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
				// log message.
				try
				{
					Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
				}
				catch
				{
					PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}
		}
		//**************************************************************************              
		///    <Description>
		///       before delete a row on grid, we need to store the line being deleted
		///    </Description>
		///    <Inputs>
		///        
		///    </Inputs>
		///    <Outputs>
		///    
		///    </Outputs>
		///    <Returns>
		///    
		///    </Returns>
		///    <Authors>
		///       DungLA
		///    </Authors>
		///    <History>
		///    16-Feb-2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		private void dgrdData_BeforeDelete(object sender, C1.Win.C1TrueDBGrid.CancelEventArgs e)
		{
			const string METHOD_NAME = THIS + ".dgrdData_BeforeDelete()";
			try
			{
				// turn of auto increment mode
				dstData.Tables[0].Columns[LINE_COL].AutoIncrement = false;
			}
			catch (Exception ex)
			{
				// displays the error message.
				PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
				// log message.
				try
				{
					Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
				}
				catch
				{
					PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}
		}
		//**************************************************************************              
		///    <Description>
		///       after delete a row on grid, we need to update amount
		///    </Description>
		///    <Inputs>
		///        
		///    </Inputs>
		///    <Outputs>
		///    
		///    </Outputs>
		///    <Returns>
		///    
		///    </Returns>
		///    <Authors>
		///       DungLA
		///    </Authors>
		///    <History>
		///    16-Feb-2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		private void dgrdData_AfterDelete(object sender, System.EventArgs e)
		{
			const string METHOD_NAME = THIS + ".dgrdData_AfterDelete()";
			try
			{
				// allow user to add new
				dgrdData.AllowAddNew = true;
				if (dgrdData.RowCount <= 1)
					return;
				// re-assign line value
				int intCount = 0;
				for (int i = 0; i < dgrdData.RowCount - 1; i++)
				{
					dgrdData[i, LINE_COL] = ++intCount;
				}
				// update total amount when user delete one line on grid
				UpdateTotalAmount();
			}
			catch (PCSException ex)
			{
				// displays the error message.
				PCSMessageBox.Show(ex.mCode, MessageBoxButtons.OK, MessageBoxIcon.Error);
				// log message.
				try
				{
					Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
				}
				catch
				{
					PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}
			catch (Exception ex)
			{
				// displays the error message.
				PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
				// log message.
				try
				{
					Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
				}
				catch
				{
					PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}
		}
		//**************************************************************************              
		///    <Description>
		///       open search form
		///    </Description>
		///    <Inputs>
		///        
		///    </Inputs>
		///    <Outputs>
		///    
		///    </Outputs>
		///    <Returns>
		///    
		///    </Returns>
		///    <Authors>
		///       DungLA
		///    </Authors>
		///    <History>
		///    07-Mar-2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		private void dgrdData_KeyDown(object sender, KeyEventArgs e)
		{
			const string METHOD_NAME = THIS + ".dgrdData_KeyDown()";
			try
			{
				switch (e.KeyCode)
				{
					case Keys.F12:
						if (dgrdData.AllowAddNew)
						{
							// move to last row of grid
							dgrdData.Row = dgrdData.RowCount;
							// set grid to EditActive mode
							dgrdData.EditActive = true;
							// line number
							int intMaxLine = 0;
							if (dgrdData[dgrdData.Row, LINE_COL].ToString() == string.Empty)
							{
								// get max line number
								if (dgrdData.Row > 0)
								{
									intMaxLine = int.Parse(dgrdData[dgrdData.Row - 1, LINE_COL].ToString());
								}
								dgrdData[dgrdData.Row, LINE_COL] = ++intMaxLine;
							}
							dgrdData[dgrdData.Row, PO_AdditionChargesTable.PURCHASEORDERMASTERID_FLD] = intPOMasterID;
							decimal decDistributionAmount = 0;
							decimal decSumAmount = CalculateTotalAmount();
							try
							{
								decDistributionAmount = decimal.Parse(txtDistributionAmount.Text.Trim());
								if (decDistributionAmount <= 0)
								{
									PCSMessageBox.Show(ErrorCode.MESSAGE_DISTRIBUTE_AMOUNT_MUST_GREATER_THAN_ZERO, MessageBoxIcon.Error);
									txtDistributionAmount.Focus();
									txtDistributionAmount.SelectAll();
									return;
								}
							}
							catch
							{
								PCSMessageBox.Show(ErrorCode.MESSAGE_INVALID_NUMERIC, MessageBoxIcon.Error);
								txtDistributionAmount.Focus();
								txtDistributionAmount.SelectAll();
								return;
							}
							if (decDistributionAmount <= decSumAmount)
							{
								PCSMessageBox.Show(ErrorCode.MESSAGE_DISTRIBUTE_AMOUNT_MUST_GREATER_THAN_SUM_AMOUNT, MessageBoxIcon.Error);
								txtDistributionAmount.Focus();
								txtDistributionAmount.SelectAll();
								return;
							}
							// amount = distribution amount - sum (amount)
							dgrdData[dgrdData.Row, PO_AdditionChargesTable.AMOUNT_FLD] = decDistributionAmount - decSumAmount;
							// set focus to SO Line
							dgrdData.Col = dgrdData.Columns.IndexOf(dgrdData.Columns[PO_PurchaseOrderDetailTable.TABLE_NAME + PO_PurchaseOrderDetailTable.LINE_FLD]);

							// if total row have data of grid equal to number of sale order detail
							// then disable add new record to grid
							int intTotalRowHaveData = dgrdData.RowCount;
							int intTotalSODetail = tblPODetails.Rows.Count;
							if (intTotalRowHaveData == intTotalSODetail)
							{
								dgrdData.AllowAddNew = false;
							}
						}
						break;
					case Keys.F4:
						dgrdData_ButtonClick(null, null);
						break;
				}
			}
			catch (Exception ex)
			{
				// displays the error message.
				PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
				// log message.
				try
				{
					Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
				}
				catch
				{
					PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}
		}
		//**************************************************************************              
		///    <Description>
		///       open search form
		///    </Description>
		///    <Inputs>
		///        
		///    </Inputs>
		///    <Outputs>
		///    
		///    </Outputs>
		///    <Returns>
		///    
		///    </Returns>
		///    <Authors>
		///       DungLA
		///    </Authors>
		///    <History>
		///    07-Mar-2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		private void dgrdData_RowColChange(object sender, RowColChangeEventArgs e)
		{
			const string METHOD_NAME = THIS + ".dgrdData_RowColChange()";
			try
			{
				// change row
				if (e.LastRow != dgrdData.Row)
				{
					// get PO Detail of current row
					int intPODetailID = 0;
					try
					{
						intPODetailID = int.Parse(dgrdData[dgrdData.Row, PO_AdditionChargesTable.PURCHASEORDERDETAILID_FLD].ToString());
					}
					catch{}
					if (intPODetailID <= 0)
						return;
					voPODetail = (PO_PurchaseOrderDetailVO)boPOAddCharge.GetPurchaseOrderDetailVO(intPODetailID);
					// if the PODetail of current row has been approved, then unable to change anything
					if (voPODetail.ApproverID > 0)
					{
						// lock all editable column
						dgrdData.Splits[0].DisplayColumns[PO_AdditionChargesTable.UNITPRICE_FLD].Locked = true;
						dgrdData.Splits[0].DisplayColumns[PO_AdditionChargesTable.QUANTITY_FLD].Locked = true;
						dgrdData.Splits[0].DisplayColumns[PO_AdditionChargesTable.AMOUNT_FLD].Locked = true;
						dgrdData.Splits[0].DisplayColumns[PO_AdditionChargesTable.VATAMOUNT_FLD].Locked = true;
						// unable to change PO, Addition Charge, Reason
						dgrdData.Splits[0].DisplayColumns[MST_ReasonTable.TABLE_NAME + MST_ReasonTable.CODE_FLD].Button = false;
						dgrdData.Splits[0].DisplayColumns[MST_ReasonTable.TABLE_NAME + MST_ReasonTable.CODE_FLD].Locked = true;
						dgrdData.Splits[0].DisplayColumns[MST_AddChargeTable.TABLE_NAME + MST_AddChargeTable.CODE_FLD].Button = false;
						dgrdData.Splits[0].DisplayColumns[MST_AddChargeTable.TABLE_NAME + MST_AddChargeTable.CODE_FLD].Locked = true;
						dgrdData.Splits[0].DisplayColumns[PO_PurchaseOrderDetailTable.TABLE_NAME + PO_PurchaseOrderDetailTable.LINE_FLD].Button = false;
						dgrdData.Splits[0].DisplayColumns[PO_PurchaseOrderDetailTable.TABLE_NAME + PO_PurchaseOrderDetailTable.LINE_FLD].Locked = true;
					}
					else
					{
						// unlock Unit price and quantity
						dgrdData.Splits[0].DisplayColumns[PO_AdditionChargesTable.UNITPRICE_FLD].Locked = false;
						dgrdData.Splits[0].DisplayColumns[PO_AdditionChargesTable.QUANTITY_FLD].Locked = false;
						dgrdData.Splits[0].DisplayColumns[PO_AdditionChargesTable.AMOUNT_FLD].Locked = false;
						dgrdData.Splits[0].DisplayColumns[PO_AdditionChargesTable.VATAMOUNT_FLD].Locked = false;
						// allow to change PO, Addition Charge, Reason
						dgrdData.Splits[0].DisplayColumns[MST_ReasonTable.TABLE_NAME + MST_ReasonTable.CODE_FLD].Button = true;
						dgrdData.Splits[0].DisplayColumns[MST_ReasonTable.TABLE_NAME + MST_ReasonTable.CODE_FLD].Locked = false;
						dgrdData.Splits[0].DisplayColumns[MST_AddChargeTable.TABLE_NAME + MST_AddChargeTable.CODE_FLD].Button = true;
						dgrdData.Splits[0].DisplayColumns[MST_AddChargeTable.TABLE_NAME + MST_AddChargeTable.CODE_FLD].Locked = false;
						dgrdData.Splits[0].DisplayColumns[PO_PurchaseOrderDetailTable.TABLE_NAME + PO_PurchaseOrderDetailTable.LINE_FLD].Button = true;
						dgrdData.Splits[0].DisplayColumns[PO_PurchaseOrderDetailTable.TABLE_NAME + PO_PurchaseOrderDetailTable.LINE_FLD].Locked = false;
					}
				}
			}
			catch (PCSException ex)
			{
				// displays the error message.
				PCSMessageBox.Show(ex.mCode, MessageBoxButtons.OK, MessageBoxIcon.Error);
				// log message.
				try
				{
					Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
				}
				catch
				{
					PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}
			catch (Exception ex)
			{
				// displays the error message.
				PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
				// log message.
				try
				{
					Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
				}
				catch
				{
					PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}
		}
		//**************************************************************************              
		///    <Description>
		///       Close the form
		///    </Description>
		///    <Inputs>
		///        
		///    </Inputs>
		///    <Outputs>
		///    
		///    </Outputs>
		///    <Returns>
		///    
		///    </Returns>
		///    <Authors>
		///       DungLA
		///    </Authors>
		///    <History>
		///    07-Mar-2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		private void POAdditionCharges_Closing(object sender, CancelEventArgs e)
		{
			const string METHOD_NAME = THIS + ".POAdditionCharges_Closing()";
			try
			{
				if (this.mFormMode != EnumAction.Default)
				{
					DialogResult dlgResult = PCSMessageBox.Show(ErrorCode.MESSAGE_QUESTION_STORE_INTO_DATABASE, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
					switch (dlgResult)
					{
						case DialogResult.Yes:
							if (!SaveData())
							{
								e.Cancel = true;
							}
							break;
						case DialogResult.No:
							e.Cancel = false;
							break;
						case DialogResult.Cancel:
							e.Cancel = true;
							break;
					}
				}
			}
			catch (PCSException ex)
			{
				// displays the error message.
				PCSMessageBox.Show(ex.mCode, MessageBoxButtons.OK, MessageBoxIcon.Error);
				// log message.
				try
				{
					Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
				}
				catch
				{
					PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}
			catch (Exception ex)
			{
				// displays the error message.
				PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
				// log message.
				try
				{
					Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
				}
				catch
				{
					PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}
		}
		#endregion

		#region private methods
		//**************************************************************************              
		///    <Description>
		///     Bind data to true db grid
		///    </Description>
		///    <Inputs>
		///        
		///    </Inputs>
		///    <Outputs>
		///    
		///    </Outputs>
		///    <Returns>
		///    
		///    </Returns>
		///    <Authors>
		///       DungLA
		///    </Authors>
		///    <History>
		///    07-Mar-2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		private void BindDataToGrid()
		{
			try
			{
				#region store all columns header for localized purpose

				string strLineHeader = dgrdData.Columns[LINE_COL].Caption;
				int intLineWidth = dgrdData.Splits[0].DisplayColumns[LINE_COL].Width;
				string strPOLineHeader = dgrdData.Columns[PO_PurchaseOrderDetailTable.TABLE_NAME + PO_PurchaseOrderDetailTable.LINE_FLD].Caption;
				int intPOLineWidth = dgrdData.Splits[0].DisplayColumns[PO_PurchaseOrderDetailTable.TABLE_NAME + PO_PurchaseOrderDetailTable.LINE_FLD].Width;
				string strAddChargeHeader = dgrdData.Columns[MST_AddChargeTable.TABLE_NAME + MST_AddChargeTable.CODE_FLD].Caption;
				string strReasonHeader = dgrdData.Columns[MST_ReasonTable.TABLE_NAME + MST_ReasonTable.CODE_FLD].Caption;
				string strQuantityHeader = dgrdData.Columns[PO_AdditionChargesTable.QUANTITY_FLD].Caption;
				string strUnitPriceHeader = dgrdData.Columns[PO_PurchaseOrderDetailTable.UNITPRICE_FLD].Caption;
				string strTaxAmountHeader = dgrdData.Columns[PO_AdditionChargesTable.AMOUNT_FLD].Caption;
				string strVATAmountHeader = dgrdData.Columns[PO_AdditionChargesTable.VATAMOUNT_FLD].Caption;
				string strTotalAmountHeader = dgrdData.Columns[PO_AdditionChargesTable.TOTALAMOUNT_FLD].Caption;

				#endregion
				
				// bind data to dgrdData
				dgrdData.DataSource = dstData.Tables[0];

				#region display columns
				foreach (C1DisplayColumn objCol in dgrdData.Splits[0].DisplayColumns)
				{
					objCol.Visible = false;
					objCol.Locked = true;
					objCol.AutoSize();
					// align center heading
					objCol.HeadingStyle.HorizontalAlignment = C1.Win.C1TrueDBGrid.AlignHorzEnum.Center;
				}
				//
				// Line
				//
				dgrdData.Splits[0].DisplayColumns[LINE_COL].Visible = true;
				dgrdData.Splits[0].DisplayColumns[LINE_COL].Width = intLineWidth;
				dgrdData.Columns[LINE_COL].Caption = strLineHeader;
				//dgrdData.Splits[0].DisplayColumns[LINE_COL].HeadingStyle.ForeColor = Color.Maroon;
				//dgrdData.Columns[LINE_COL].Aggregate = AggregateEnum.Count;
				//
				// PO Line
				//
				dgrdData.Splits[0].DisplayColumns[PO_PurchaseOrderDetailTable.TABLE_NAME + PO_PurchaseOrderDetailTable.LINE_FLD].Visible = true;
				dgrdData.Splits[0].DisplayColumns[PO_PurchaseOrderDetailTable.TABLE_NAME + PO_PurchaseOrderDetailTable.LINE_FLD].Width = intPOLineWidth;
				dgrdData.Columns[PO_PurchaseOrderDetailTable.TABLE_NAME + PO_PurchaseOrderDetailTable.LINE_FLD].Caption = strPOLineHeader;
				dgrdData.Columns[PO_PurchaseOrderDetailTable.TABLE_NAME + PO_PurchaseOrderDetailTable.LINE_FLD].Editor = txtCellValue;
				dgrdData.Splits[0].DisplayColumns[PO_PurchaseOrderDetailTable.TABLE_NAME + PO_PurchaseOrderDetailTable.LINE_FLD].HeadingStyle.ForeColor = Color.Maroon;
				dgrdData.Splits[0].DisplayColumns[PO_PurchaseOrderDetailTable.TABLE_NAME + PO_PurchaseOrderDetailTable.LINE_FLD].Button = true;
				dgrdData.Splits[0].DisplayColumns[PO_PurchaseOrderDetailTable.TABLE_NAME + PO_PurchaseOrderDetailTable.LINE_FLD].Locked = false;
				//
				// Addition Charge
				//
				dgrdData.Splits[0].DisplayColumns[MST_AddChargeTable.TABLE_NAME + MST_AddChargeTable.CODE_FLD].Visible = true;
				dgrdData.Columns[MST_AddChargeTable.TABLE_NAME + MST_AddChargeTable.CODE_FLD].Caption = strAddChargeHeader;
				dgrdData.Columns[MST_AddChargeTable.TABLE_NAME + MST_AddChargeTable.CODE_FLD].Editor = txtCellValue;
				dgrdData.Splits[0].DisplayColumns[MST_AddChargeTable.TABLE_NAME + MST_AddChargeTable.CODE_FLD].HeadingStyle.ForeColor = Color.Maroon;
				dgrdData.Splits[0].DisplayColumns[MST_AddChargeTable.TABLE_NAME + MST_AddChargeTable.CODE_FLD].Button = true;
				dgrdData.Splits[0].DisplayColumns[MST_AddChargeTable.TABLE_NAME + MST_AddChargeTable.CODE_FLD].Locked = false;
				//
				// Reason
				//
				dgrdData.Splits[0].DisplayColumns[MST_ReasonTable.TABLE_NAME + MST_ReasonTable.CODE_FLD].Visible = true;
				dgrdData.Columns[MST_ReasonTable.TABLE_NAME + MST_ReasonTable.CODE_FLD].Caption = strReasonHeader;
				dgrdData.Columns[MST_ReasonTable.TABLE_NAME + MST_ReasonTable.CODE_FLD].Editor = txtCellValue;
				//dgrdData.Splits[0].DisplayColumns[MST_ReasonTable.TABLE_NAME + MST_ReasonTable.CODE_FLD].HeadingStyle.ForeColor = Color.Maroon;
				dgrdData.Splits[0].DisplayColumns[MST_ReasonTable.TABLE_NAME + MST_ReasonTable.CODE_FLD].Button = true;
				dgrdData.Splits[0].DisplayColumns[MST_ReasonTable.TABLE_NAME + MST_ReasonTable.CODE_FLD].Locked = false;
				//
				// Quantity
				//
				dgrdData.Splits[0].DisplayColumns[PO_AdditionChargesTable.QUANTITY_FLD].Visible = true;
				dgrdData.Columns[PO_AdditionChargesTable.QUANTITY_FLD].Caption = strQuantityHeader;
				dgrdData.Splits[0].DisplayColumns[PO_AdditionChargesTable.QUANTITY_FLD].Locked = false;
				dgrdData.Columns[PO_AdditionChargesTable.QUANTITY_FLD].NumberFormat = Constants.DECIMAL_NUMBERFORMAT;
				//
				// Unit Price
				//
				dgrdData.Splits[0].DisplayColumns[PO_AdditionChargesTable.UNITPRICE_FLD].Visible = true;
				dgrdData.Columns[PO_AdditionChargesTable.UNITPRICE_FLD].Caption = strUnitPriceHeader;
				dgrdData.Splits[0].DisplayColumns[PO_AdditionChargesTable.UNITPRICE_FLD].Locked = false;
				dgrdData.Columns[PO_AdditionChargesTable.UNITPRICE_FLD].NumberFormat = Constants.DECIMAL_NUMBERFORMAT;
				//
				// Tax Amount
				//
				dgrdData.Splits[0].DisplayColumns[PO_AdditionChargesTable.AMOUNT_FLD].Visible = true;
				dgrdData.Splits[0].DisplayColumns[PO_AdditionChargesTable.AMOUNT_FLD].Locked = true;
				dgrdData.Columns[PO_AdditionChargesTable.AMOUNT_FLD].Caption = strTaxAmountHeader;
				dgrdData.Columns[PO_AdditionChargesTable.AMOUNT_FLD].NumberFormat = Constants.DECIMAL_NUMBERFORMAT;
				//
				// VAT Amount
				//
				dgrdData.Splits[0].DisplayColumns[PO_AdditionChargesTable.VATAMOUNT_FLD].Visible = true;
				dgrdData.Splits[0].DisplayColumns[PO_AdditionChargesTable.VATAMOUNT_FLD].Locked = true;
				dgrdData.Columns[PO_AdditionChargesTable.VATAMOUNT_FLD].Caption = strVATAmountHeader;
				dgrdData.Columns[PO_AdditionChargesTable.VATAMOUNT_FLD].NumberFormat = Constants.DECIMAL_NUMBERFORMAT;
				//
				// Total Amount
				//
				dgrdData.Splits[0].DisplayColumns[PO_AdditionChargesTable.TOTALAMOUNT_FLD].Visible = true;
				dgrdData.Splits[0].DisplayColumns[PO_AdditionChargesTable.TOTALAMOUNT_FLD].Locked = true;
				dgrdData.Columns[PO_AdditionChargesTable.TOTALAMOUNT_FLD].Caption = strTotalAmountHeader;
				dgrdData.Columns[PO_AdditionChargesTable.TOTALAMOUNT_FLD].NumberFormat = Constants.DECIMAL_NUMBERFORMAT;
				//Align center all caption of columns

				#endregion

				// if total row have data of grid equal to number of sale order detail
				// then disable add new record to grid
				int intTotalRowHaveData = dgrdData.RowCount;
				if (intTotalRowHaveData == tblPODetails.Rows.Count)
				{
					dgrdData.AllowAddNew = false;
				}
			}
			catch (PCSException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		//**************************************************************************              
		///    <Description>
		///     This method uses to calculate Total Amount
		///    </Description>
		///    <Inputs>
		///        
		///    </Inputs>
		///    <Outputs>
		///    
		///    </Outputs>
		///    <Returns>
		///    
		///    </Returns>
		///    <Authors>
		///       DungLA
		///    </Authors>
		///    <History>
		///    07-Mar-2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		private decimal CalculateTotalAmount()
		{
			try
			{
				// get rows count of grid
				int intRowsCount = this.dgrdData.RowCount;
				decimal decTotalValue = 0;
				
				for (int i = 0; i < intRowsCount; i++)
				{
					try
					{
						decTotalValue += decimal.Parse(this.dgrdData[i, PO_AdditionChargesTable.TOTALAMOUNT_FLD].ToString());
					}
					catch {}
				}
				return decTotalValue;
			}
			catch (PCSException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		//**************************************************************************              
		///    <Description>
		///     This method uses to calculate Total VAT Amount
		///    </Description>
		///    <Inputs>
		///        
		///    </Inputs>
		///    <Outputs>
		///    
		///    </Outputs>
		///    <Returns>
		///    
		///    </Returns>
		///    <Authors>
		///       DungLA
		///    </Authors>
		///    <History>
		///    07-Mar-2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		private decimal CalculateTotalVATAmount()
		{
			try
			{
				// get rows count of grid
				int intRowsCount = this.dgrdData.RowCount;
				decimal decTotalValue = 0;
				
				// calculate value
				for (int i = 0; i < intRowsCount; i++)
				{
					try
					{
						decTotalValue += decimal.Parse(this.dgrdData[i, PO_AdditionChargesTable.VATAMOUNT_FLD].ToString());
					}
					catch {}
				}
				return decTotalValue;
			}
			catch (PCSException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		//**************************************************************************              
		///    <Description>
		///     This method uses to calculate Total Tax Amount
		///    </Description>
		///    <Inputs>
		///        
		///    </Inputs>
		///    <Outputs>
		///    
		///    </Outputs>
		///    <Returns>
		///    
		///    </Returns>
		///    <Authors>
		///       DungLA
		///    </Authors>
		///    <History>
		///    07-Mar-2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		private decimal CalculateTotalTaxAmount()
		{
			try
			{
				// get rows count of grid
				int intRowsCount = this.dgrdData.RowCount;
				decimal decTotalValue = 0;
				
				// calculate value
				for (int i = 0; i < intRowsCount; i++)
				{
					try
					{
						decTotalValue += decimal.Parse(this.dgrdData[i, PO_AdditionChargesTable.AMOUNT_FLD].ToString());
					}
					catch {}
				}
				return decTotalValue;
			}
			catch (PCSException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		//**************************************************************************              
		///    <Description>
		///     This method uses to update total amount (NET, VAT, Amount)
		///    </Description>
		///    <Inputs>
		///        
		///    </Inputs>
		///    <Outputs>
		///    
		///    </Outputs>
		///    <Returns>
		///    
		///    </Returns>
		///    <Authors>
		///       DungLA
		///    </Authors>
		///    <History>
		///    07-Mar-2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		private void UpdateTotalAmount()
		{
			try
			{
				// calculate Total VAT Amount
				decimal decTotalVATAmount = CalculateTotalVATAmount();
				txtTotalVATAmount.Text = decTotalVATAmount.ToString(Constants.DECIMAL_NUMBERFORMAT);
				// calculate Total Amount
				decimal decTotalAmount = CalculateTotalAmount();
				txtTotalAmount.Text = decTotalAmount.ToString(Constants.DECIMAL_NUMBERFORMAT);
				// calculate Total Tax Amount
				decimal decTotalTaxAmount = CalculateTotalTaxAmount();
				txtTotalTaxAmount.Text = decTotalTaxAmount.ToString(Constants.DECIMAL_NUMBERFORMAT);
			}
			catch (PCSException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		//**************************************************************************              
		///    <Description>
		///     This method uses to update Amount, VATAmount, Total Amount incase of charge by manual
		///    </Description>
		///    <Inputs>
		///        
		///    </Inputs>
		///    <Outputs>
		///    
		///    </Outputs>
		///    <Returns>
		///    
		///    </Returns>
		///    <Authors>
		///       DungLA
		///    </Authors>
		///    <History>
		///    16-Feb-2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		private void UpdateAmount(string pstrBy, bool pblnForOneRow)
		{
			try
			{
				decimal decAmount = 0;
				decimal decQuantity = 0;
				decimal decUnitPrice = 0;
				decimal decAddChargeVAT = 0;
				decimal decVATAmount = 0;
				decimal decTotalAmount = 0;
				decimal decTotalQuantity = 0;
				decimal decTotalUniPrice = 0;
				decimal decDistributionAmount = 0;
				int intAddChargeID = 0;
				// get distribution amount
				try
				{
					decDistributionAmount = decimal.Parse(txtDistributionAmount.Value.ToString().Trim());
					if (decDistributionAmount <= 0)
					{
						PCSMessageBox.Show(ErrorCode.MESSAGE_DISTRIBUTE_AMOUNT_MUST_GREATER_THAN_ZERO, MessageBoxIcon.Error);
						txtDistributionAmount.Focus();
						txtDistributionAmount.SelectAll();
						throw new Exception();
					}
				}
				catch
				{
					dgrdData[dgrdData.Row, dgrdData.Col] = strSelectedValue;
					return;
				}
				int intRowsCount = dgrdData.RowCount;
				// we get total quantity, total unit price
				for (int i = 0; i < intRowsCount; i++)
				{
					// get total quantity of all rows
					try
					{
						if (!pblnForOneRow)
							decTotalQuantity += decimal.Parse(dgrdData[i, PO_PurchaseOrderDetailTable.TABLE_NAME + PO_AdditionChargesTable.QUANTITY_FLD].ToString());
						else
							decTotalQuantity += decimal.Parse(dgrdData[i,  PO_AdditionChargesTable.QUANTITY_FLD].ToString());
					}
					catch
					{
					}
					// get total unit price of all rows
					try
					{
						if (!pblnForOneRow)
							decTotalUniPrice += decimal.Parse(dgrdData[i, PO_PurchaseOrderDetailTable.TABLE_NAME + PO_AdditionChargesTable.UNITPRICE_FLD].ToString());
						else
							decTotalUniPrice += decimal.Parse(dgrdData[i, PO_AdditionChargesTable.UNITPRICE_FLD].ToString());
						
					}
					catch
					{
					}
				}
//				if (radByQuantity.Checked && (decTotalQuantity <= 0))
//					return;
//				if (radByPrice.Checked && (decTotalUniPrice <= 0))
//					return;
				// we have total quantity and total unit price
				// now fill value to Amount, VAT Amount, Total Amount cell of grid
				if (pblnForOneRow)
				{
					try
					{
						decQuantity = decimal.Parse(dgrdData[dgrdData.Row, PO_AdditionChargesTable.QUANTITY_FLD].ToString());
						//decQuantity = decimal.Parse(dgrdData[dgrdData.Row, PO_PurchaseOrderDetailTable.TABLE_NAME + PO_AdditionChargesTable.QUANTITY_FLD].ToString());
					}
					catch
					{
						decQuantity = 0;
					}
					try
					{
						decUnitPrice = decimal.Parse(dgrdData[dgrdData.Row, PO_AdditionChargesTable.UNITPRICE_FLD].ToString());
						//decUnitPrice = decimal.Parse(dgrdData[dgrdData.Row, PO_PurchaseOrderDetailTable.TABLE_NAME + PO_AdditionChargesTable.UNITPRICE_FLD].ToString());
					}
					catch
					{
						decUnitPrice = 0;
					}
					try
					{
						decAmount = decimal.Parse(dgrdData[dgrdData.Row, PO_AdditionChargesTable.AMOUNT_FLD].ToString());
					}
					catch
					{
						decAmount = 0;
					}
					if(decUnitPrice != 0 && decQuantity != 0)
						decAmount = decUnitPrice * decQuantity;
//					switch (pstrBy)
//					{
//						case BY_PRICE:
//							// Amount = (Unit Price/Total Unit Price) * Distribution Amount)
//							//decAmount = Math.Round((decUnitPrice / decTotalUniPrice) * decDistributionAmount, DECIMAL_ROUND);
//							decAmount = decUnitPrice * decQuantity;
//							break;
//						case BY_QUANTITY:
//							// Amount = (Quantity/Total Quantity) * Distribution Amount)
//							decAmount = Math.Round((decQuantity / decTotalQuantity) * decDistributionAmount, DECIMAL_ROUND);
//							break;
//						case BY_MANUAL:
//							// Amount = Quantity * Unit Price
//							decAmount = decQuantity * decUnitPrice;
//							break;
//					}
					// get AddChargeID
					try
					{
						intAddChargeID = int.Parse(dgrdData[dgrdData.Row, SO_AdditionChargeTable.ADDCHARGEID_FLD].ToString());
					}
					catch{}
					if (intAddChargeID > 0)
					{
						// get addition charge vo
						voAddCharge = (MST_AddChargeVO) boPOAddCharge.GetAddCharge(intAddChargeID);
						decAddChargeVAT = voAddCharge.VAT;
					}
					// get Addition Charge VAT
					decAddChargeVAT = decAddChargeVAT;
					// VAT Amount =  Amount * Addition Charge VAT / 100
					decVATAmount = Math.Round(decAmount * decAddChargeVAT / VAT_NUMBER, DECIMAL_ROUND);
					// Total Amount = Amount + VAT Amount
					decTotalAmount = Math.Round(decAmount + decVATAmount, DECIMAL_ROUND);
					// fill data to cell
					dgrdData[dgrdData.Row, PO_AdditionChargesTable.AMOUNT_FLD] = decAmount;
					dgrdData[dgrdData.Row, PO_AdditionChargesTable.VATAMOUNT_FLD] = decVATAmount;
					dgrdData[dgrdData.Row, PO_AdditionChargesTable.TOTALAMOUNT_FLD] = decTotalAmount;
				}
				else
				{
					if (radByQuantity.Checked && (decTotalQuantity <= 0))
						return;
					if (radByPrice.Checked && (decTotalUniPrice <= 0))
						return;
					for (int i = 0; i < intRowsCount; i++)
					{
						// get PO Detail of current row
						int intPODetailID = 0;
						try
						{
							intPODetailID = int.Parse(dgrdData[i, PO_AdditionChargesTable.PURCHASEORDERDETAILID_FLD].ToString());
						}
						catch{}
						if (intPODetailID <= 0)
							continue;
						voPODetail = (PO_PurchaseOrderDetailVO)boPOAddCharge.GetPurchaseOrderDetailVO(intPODetailID);
						// if current line was approved then continue to next line
						if (voPODetail.ApproverID > 0)
							continue;
						try
						{
							//decQuantity = decimal.Parse(dgrdData[i, PO_AdditionChargesTable.QUANTITY_FLD].ToString());
							decQuantity = decimal.Parse(dgrdData[i, PO_PurchaseOrderDetailTable.TABLE_NAME + PO_AdditionChargesTable.QUANTITY_FLD].ToString());
						}
						catch
						{
							decQuantity = 0;
						}
						try
						{
							//decUnitPrice = decimal.Parse(dgrdData[i, PO_AdditionChargesTable.UNITPRICE_FLD].ToString());
							decUnitPrice = decimal.Parse(dgrdData[i, PO_PurchaseOrderDetailTable.TABLE_NAME + PO_AdditionChargesTable.UNITPRICE_FLD].ToString());
						}
						catch
						{
							decUnitPrice = 0;
						}
						switch (pstrBy)
						{
							case BY_PRICE:
								// Amount = (Unit Price/Total Unit Price) * Distribution Amount)
								decAmount = Math.Round((decUnitPrice / decTotalUniPrice) * decDistributionAmount, DECIMAL_ROUND);
								break;
							case BY_QUANTITY:
								// Amount = (Quantity/Total Quantity) * Distribution Amount)
								decAmount = Math.Round((decQuantity / decTotalQuantity) * decDistributionAmount, DECIMAL_ROUND);
								break;
							case BY_MANUAL:
								// Amount = Quantity * Unit Price
								decAmount = decQuantity * decUnitPrice;
								break;
						}
						// get AddChargeID
						try
						{
							intAddChargeID = int.Parse(dgrdData[dgrdData.Row, SO_AdditionChargeTable.ADDCHARGEID_FLD].ToString());
						}
						catch{}
						if (intAddChargeID > 0)
						{
							// get addition charge vo
							voAddCharge = (MST_AddChargeVO) boPOAddCharge.GetAddCharge(intAddChargeID);
							decAddChargeVAT = voAddCharge.VAT;
						}
						// get Addition Charge VAT
						decAddChargeVAT = decAddChargeVAT;
						// VAT Amount =  Amount * Addition Charge VAT / 100
						decVATAmount = Math.Round(decAmount * decAddChargeVAT / VAT_NUMBER, DECIMAL_ROUND);
						// Total Amount = Amount + VAT Amount
						decTotalAmount = Math.Round(decAmount + decVATAmount, DECIMAL_ROUND);
						// fill data to cell
						dgrdData[i, PO_AdditionChargesTable.AMOUNT_FLD] = decAmount;
						dgrdData[i, PO_AdditionChargesTable.VATAMOUNT_FLD] = decVATAmount;
						dgrdData[i, PO_AdditionChargesTable.TOTALAMOUNT_FLD] = decTotalAmount;
					}
				}
				// update TotalAmount, TotalVATAmount, TotalNETAmount;
				UpdateTotalAmount();
			}
			catch (PCSException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		//**************************************************************************              
		///    <Description>
		///       - Save into Database and show success message
		///    </Description>
		///    <Inputs>
		///        
		///    </Inputs>
		///    <Outputs>
		///			true if save successfull
		///			false if failed
		///    </Outputs>
		///    <Returns>
		///			bool
		///    </Returns>
		///    <Authors>
		///       DungLA
		///    </Authors>
		///    <History>
		///    17-Feb-2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		private bool SaveData()
		{
			try
			{
				// update data source
				dgrdData.UpdateData();
				// update data base
				boPOAddCharge.UpdateDataSet(dstData);
				// refresh the grid
				dgrdData.Refresh();
				// turn off edit mode
				mFormMode = EnumAction.Default;
				return true;
			}
			catch (PCSException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		//**************************************************************************              
		///    <Description>
		///       This method use to assign Enter and Leave event for all editable control on form
		///    </Description>
		///    <Inputs>
		///       Control to assign
		///    </Inputs>
		///    <Outputs>
		///
		///    </Outputs>
		///    <Returns>
		///       
		///    </Returns>
		///    <Authors>
		///       DungLA
		///    </Authors>
		///    <History>
		///       29-Mar-2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		private void AssignEvent(Control pobjControl)
		{
			foreach (Control objControl in pobjControl.Controls)
			{
				if (objControl.HasChildren)
				{
					AssignEvent(objControl);
				}
				if (((objControl is TextBox) || (objControl is ComboBox) ||
					(objControl is DateTimePicker) || (objControl is C1Combo) ||
					(objControl is C1TextBox) || (objControl is C1DateEdit) ||
					(objControl is C1NumericEdit) || (objControl is C1DropDownControl)) && (!objControl.Equals(txtCellValue)))
				{
					objControl.Enter += new EventHandler(this.OnEnterControl);
					objControl.Leave += new EventHandler(this.OnLeaveControl);
				}
			}
		}
		//**************************************************************************              
		///    <Description>
		///       This method use to format control when Enter event raise in the form
		///    </Description>
		///    <Inputs>
		///       Control to format
		///    </Inputs>
		///    <Outputs>
		///
		///    </Outputs>
		///    <Returns>
		///       
		///    </Returns>
		///    <Authors>
		///       DungLA
		///    </Authors>
		///    <History>
		///       04-Jan-2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		private void OnEnterControl(object sender, EventArgs e)
		{
			const string METHOD_NAME = THIS + ".OnEnterControl()";
			try
			{
				#region // HACK: DEL dungla 10-14-2005

//				if (sender.GetType().Equals(typeof(TextBox)))
//				{
//					objBackColor = ((TextBox)sender).BackColor;
//					objForeColor = ((TextBox)sender).ForeColor;
//				}
//				else if (sender.GetType().Equals(typeof(C1NumericEdit)))
//				{
//					objBackColor = ((C1NumericEdit)sender).BackColor;
//					objForeColor = ((C1NumericEdit)sender).ForeColor;
//				}

				#endregion // END: DEL dungla 10-14-2005

				FormControlComponents.OnEnterControl(sender, e);
			}
			catch (Exception ex)
			{
				// displays the error message.
				PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
				// log message.
				try
				{
					Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
				}
				catch
				{
					PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}
		}
		//**************************************************************************              
		///    <Description>
		///       This method use to format control when Leave event raise in the form
		///    </Description>
		///    <Inputs>
		///       Control to format
		///    </Inputs>
		///    <Outputs>
		///
		///    </Outputs>
		///    <Returns>
		///       
		///    </Returns>
		///    <Authors>
		///       DungLA
		///    </Authors>
		///    <History>
		///       04-Jan-2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		private void OnLeaveControl(object sender, EventArgs e)
		{
			const string METHOD_NAME = THIS + ".OnLeaveControl()";
			try
			{
				// validate data if has changed
				if (sender.GetType().Equals(typeof(C1NumericEdit)))
				{
					C1NumericEdit txtSender = (C1NumericEdit)sender;
					if (txtSender.Modified)
					{
						if (txtSender.Text.Trim() != string.Empty)
						{
							if (txtSender.Equals(txtDistributionAmount))
							{
								try
								{
									decimal decDistributionAmount = decimal.Parse(txtDistributionAmount.Value.ToString().Trim());
									if (decDistributionAmount <= 0)
									{
										PCSMessageBox.Show(ErrorCode.MESSAGE_DISTRIBUTE_AMOUNT_MUST_GREATER_THAN_ZERO, MessageBoxIcon.Error);
										txtDistributionAmount.Focus();
										txtDistributionAmount.SelectAll();
										return;
									}
								}
								catch
								{
									PCSMessageBox.Show(ErrorCode.MESSAGE_INVALID_NUMERIC, MessageBoxButtons.OK, MessageBoxIcon.Error);
									this.txtDistributionAmount.Focus();
									this.txtDistributionAmount.Select();
									return;
								}
							}
						}
					}
				}
			// HACK: SonHT 2005-10-13
			//				FormControlComponents.OnLeaveControl(sender, e, objForeColor, objBackColor);
			// END: SonHT 2005-10-13
			}
			catch (Exception ex)
			{
				// displays the error message.
				PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
				// log message.
				try
				{
					Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
				}
				catch
				{
					PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}
		}
		//**************************************************************************              
		///    <Description>
		///       This method use to get row and fill data to grid
		///       if GetRows return 0 then OpenSearchForm(false)
		///       else if GetRows return 1 then fill data to grid
		///       else if GetRows return more than 1 record then OpenSearchForm(true)
		///    </Description>
		///    <Inputs>
		///       
		///    </Inputs>
		///    <Outputs>
		///
		///    </Outputs>
		///    <Returns>
		///       
		///    </Returns>
		///    <Authors>
		///       DungLA
		///    </Authors>
		///    <History>
		///       16-04-2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		private void FillDataToGrid()
		{
			try
			{
				string strColumn = dgrdData.Columns[dgrdData.Col].DataField;
				if (!strColumn.Equals(PO_PurchaseOrderDetailTable.TABLE_NAME + PO_PurchaseOrderDetailTable.LINE_FLD)
					&& !strColumn.Equals(MST_AddChargeTable.TABLE_NAME + MST_AddChargeTable.CODE_FLD) 
					&& !strColumn.Equals(MST_ReasonTable.TABLE_NAME + MST_ReasonTable.CODE_FLD))
				{
					return;
				}
				string strValue = dgrdData[dgrdData.Row, dgrdData.Col].ToString().Trim();
				DataRowView drvData = null;
				string strExpression = string.Empty;
				int intRowCount = dgrdData.RowCount;
				switch (strColumn)
				{
					// PO line
					case PO_PurchaseOrderDetailTable.TABLE_NAME + PO_PurchaseOrderDetailTable.LINE_FLD:
						// ignore all po line has been approved
						strExpression = Constants.WHERE_KEYWORD + Constants.WHITE_SPACE
							+ PO_PurchaseOrderDetailTable.PURCHASEORDERMASTERID_FLD + Constants.EQUAL + voPOMaster.PurchaseOrderMasterID.ToString()
							+ Constants.AND + OPEN_QUOTE + PO_PurchaseOrderDetailTable.APPROVERID_FLD + SMALLER_THAN + 1 + OR + PO_PurchaseOrderDetailTable.APPROVERID_FLD + IS_NULL + CLOSE_QUOTE; 
						drvData = FormControlComponents.OpenSearchForm(PO_PurchaseOrderDetailTable.TABLE_NAME, PO_PurchaseOrderDetailTable.LINE_FLD, strValue, strExpression);
						if (drvData != null)
							FillPOData(drvData.Row, intRowCount);
						break;
					// Addition Charge
					case (MST_AddChargeTable.TABLE_NAME + MST_AddChargeTable.CODE_FLD):
						drvData = FormControlComponents.OpenSearchForm(MST_AddChargeTable.TABLE_NAME, MST_AddChargeTable.CODE_FLD, strValue, null, true);
						if (drvData != null)
							FillAdditionChargeData(drvData.Row);
						break;
					// Reason
					case (MST_ReasonTable.TABLE_NAME + MST_ReasonTable.CODE_FLD):
						drvData = FormControlComponents.OpenSearchForm(MST_ReasonTable.TABLE_NAME, MST_ReasonTable.CODE_FLD, strValue, null, true);
						if (drvData != null)
							FillReasonData(drvData.Row);
						break;
				}
			}
			catch (PCSException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		//**************************************************************************              
		///    <Description>
		///       This method use to fill purchase order detail data to grid
		///    </Description>
		///    <Inputs>
		///       
		///    </Inputs>
		///    <Outputs>
		///
		///    </Outputs>
		///    <Returns>
		///       
		///    </Returns>
		///    <Authors>
		///       DungLA
		///    </Authors>
		///    <History>
		///       16-04-2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		private void FillPOData(DataRow pdrowData, int pintRowCount)
		{
			try
			{
				// get PurchaseOrderDetailVO
				if ((pdrowData[PO_PurchaseOrderDetailTable.PURCHASEORDERDETAILID_FLD] != null) && (pdrowData[PO_PurchaseOrderDetailTable.PURCHASEORDERDETAILID_FLD].ToString() != string.Empty))
					voPODetail = (PO_PurchaseOrderDetailVO)boPOAddCharge.GetPurchaseOrderDetailVO(int.Parse(pdrowData[PO_PurchaseOrderDetailTable.PURCHASEORDERDETAILID_FLD].ToString()));
				else
					return;
				// set grid to EditActive mode
				dgrdData.EditActive = true;
				// line number
				int intMaxLine = 0;
				if (dgrdData[dgrdData.Row, LINE_COL].ToString() == string.Empty)
				{
					// get max line number
					if (dgrdData.Row > 0)
					{
						intMaxLine = int.Parse(dgrdData[dgrdData.Row - 1, LINE_COL].ToString());
					}
					dgrdData[dgrdData.Row, LINE_COL] = ++intMaxLine;
				}
				// po line
				dgrdData[dgrdData.Row, PO_PurchaseOrderDetailTable.TABLE_NAME + PO_PurchaseOrderDetailTable.LINE_FLD] = voPODetail.Line;
				
				// po detail id
				dgrdData[dgrdData.Row, PO_AdditionChargesTable.PURCHASEORDERDETAILID_FLD] = voPODetail.PurchaseOrderDetailID;
				if (dgrdData.AllowAddNew)
				{
					pintRowCount = dgrdData.RowCount;
					// if total row have data of grid equal to number of purchase order detail
					// then disable add new record to grid
					if (pintRowCount == tblPODetails.Rows.Count)
						dgrdData.AllowAddNew = false;
					else
						dgrdData.AllowAddNew = true;
				}
				if (radByPrice.Checked)
					UpdateAmount(BY_PRICE, true);
				else if (radByQuantity.Checked)
					UpdateAmount(BY_QUANTITY, true);
				else if (radByManual.Checked)
					UpdateAmount(BY_MANUAL, true);
			}
			catch (PCSException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		//**************************************************************************              
		///    <Description>
		///       This method use to fill addition charge data to grid
		///    </Description>
		///    <Inputs>
		///       
		///    </Inputs>
		///    <Outputs>
		///
		///    </Outputs>
		///    <Returns>
		///       
		///    </Returns>
		///    <Authors>
		///       DungLA
		///    </Authors>
		///    <History>
		///       16-04-2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		private void FillAdditionChargeData(DataRow pdrowData)
		{
			try
			{
				if ((pdrowData[MST_AddChargeTable.ADDCHARGEID_FLD] != null) && (pdrowData[MST_AddChargeTable.ADDCHARGEID_FLD].ToString() != string.Empty))
					voAddCharge = (MST_AddChargeVO) boPOAddCharge.GetAddCharge(int.Parse(pdrowData[MST_AddChargeTable.ADDCHARGEID_FLD].ToString()));
				else
					return;
				// set grid to EditActive mode
				dgrdData.EditActive = true;
				dgrdData[dgrdData.Row, MST_AddChargeTable.TABLE_NAME + MST_AddChargeTable.CODE_FLD] = voAddCharge.Code;
				dgrdData[dgrdData.Row, PO_AdditionChargesTable.ADDCHARGEID_FLD] = voAddCharge.AddChargeID;
				// update amount on grid
				if (radByPrice.Checked)
					UpdateAmount(BY_PRICE, true);
				else if (radByQuantity.Checked)
					UpdateAmount(BY_QUANTITY, true);
				else if (radByManual.Checked)
					UpdateAmount(BY_MANUAL, true);
			}
			catch (PCSException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		//**************************************************************************              
		///    <Description>
		///       This method use to fill reason data to grid
		///    </Description>
		///    <Inputs>
		///       
		///    </Inputs>
		///    <Outputs>
		///
		///    </Outputs>
		///    <Returns>
		///       
		///    </Returns>
		///    <Authors>
		///       DungLA
		///    </Authors>
		///    <History>
		///       16-04-2005
		///    </History>
		///    <Notes>
		///    </Notes>
		//**************************************************************************
		private void FillReasonData(DataRow pdrowData)
		{
			try
			{
				if ((pdrowData[MST_ReasonTable.REASONID_FLD] != null) && (pdrowData[MST_ReasonTable.REASONID_FLD].ToString() != string.Empty))
					voReason = (MST_ReasonVO) boPOAddCharge.GetReasonVO(int.Parse(pdrowData[MST_ReasonTable.REASONID_FLD].ToString()));
				else
					return;
				// set grid to EditActive mode
				dgrdData.EditActive = true;
				dgrdData[dgrdData.Row, MST_ReasonTable.TABLE_NAME + MST_ReasonTable.CODE_FLD] = voReason.Code;
				dgrdData[dgrdData.Row, PO_AdditionChargesTable.REASONID_FLD] = voReason.ReasonID;
			}
			catch (PCSException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		#endregion

		private void dgrdData_BeforeColUpdate(object sender, C1.Win.C1TrueDBGrid.BeforeColUpdateEventArgs e)
		{
			const string METHOD_NAME = THIS + ".dgrdData_BeforeColUpdate()";
			try
			{
				string strExpression = string.Empty;
				DataRowView drvData = null;
				int intRowCount = dgrdData.RowCount;
				string strValue = dgrdData[dgrdData.Row, dgrdData.Col].ToString().Trim();
				switch (e.Column.DataColumn.DataField)
				{
						// PO line
					case PO_PurchaseOrderDetailTable.TABLE_NAME + PO_PurchaseOrderDetailTable.LINE_FLD:
						// ignore all po line has been approved
						strExpression = Constants.WHERE_KEYWORD + Constants.WHITE_SPACE
							+ PO_PurchaseOrderDetailTable.PURCHASEORDERMASTERID_FLD + Constants.EQUAL + voPOMaster.PurchaseOrderMasterID.ToString()
							+ Constants.AND + OPEN_QUOTE + PO_PurchaseOrderDetailTable.APPROVERID_FLD + SMALLER_THAN + 1 + OR + PO_PurchaseOrderDetailTable.APPROVERID_FLD + IS_NULL + CLOSE_QUOTE; 
						drvData = FormControlComponents.OpenSearchForm(PO_PurchaseOrderDetailTable.TABLE_NAME, PO_PurchaseOrderDetailTable.LINE_FLD, strValue, strExpression);
						if (drvData != null)
							FillPOData(drvData.Row, intRowCount);
						else
							e.Cancel = true;
						break;
						// Addition Charge
					case (MST_AddChargeTable.TABLE_NAME + MST_AddChargeTable.CODE_FLD):
						drvData = FormControlComponents.OpenSearchForm(MST_AddChargeTable.TABLE_NAME, MST_AddChargeTable.CODE_FLD, strValue, null, true);
						if (drvData != null)
							FillAdditionChargeData(drvData.Row);
						else
							e.Cancel = true;
						break;
						// Reason
					case (MST_ReasonTable.TABLE_NAME + MST_ReasonTable.CODE_FLD):
						drvData = FormControlComponents.OpenSearchForm(MST_ReasonTable.TABLE_NAME, MST_ReasonTable.CODE_FLD, strValue, null, true);
						if (drvData != null)
							FillReasonData(drvData.Row);
						else
							e.Cancel = true;
						break;
				}
			}
			catch (PCSException ex)
			{
				PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
				try
				{
					Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
				}
				catch
				{
					PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
				}
			}
			catch (Exception ex)
			{
				PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
				try
				{
					Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
				}
				catch
				{
					PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
				}
			}
		}
	}
}
